package com.cookor.poi.modules.phy.service;

import com.cookor.poi.common.persistence.DataEntity;
import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.service.CrudService;
import com.cookor.poi.common.utils.DateUtils;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.modules.phy.dao.TActionDao;
import com.cookor.poi.modules.phy.entity.TAction;
import com.cookor.poi.modules.phy.entity.TBookNo;
import com.cookor.poi.modules.phy.entity.TDoctor;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
public class TActionService extends CrudService<TActionDao, TAction> {
  @Autowired
  private TBookNoService tBookNoService;
  
  @Autowired
  private TDoctorService tDoctorService;
  
  public TAction get(String id) {
    return (TAction)super.get(id);
  }
  
  public List<TAction> findList(TAction tAction) {
    return super.findList( tAction);
  }
  
  public Page<TAction> findPage(Page<TAction> page, TAction tAction) {
    return super.findPage(page,  tAction);
  }
  
  @Transactional(readOnly = false)
  public void save(TAction tAction) {
    super.save( tAction);
  }
  
  @Transactional(readOnly = false, rollbackFor = {Exception.class})
  public void mySave(TAction tAction) throws Exception {
    if (tAction.getEndDate().getTime() <= tAction.getBeginDate().getTime())
      throw new Exception("活动起止时间设置有误"); 
    if (tAction.getEndDate().getTime() > tAction.getActionDate().getTime())
      throw new Exception("活动结束时间不得大于坐诊时间"); 
    if (tAction.getBeginDate().getTime() > (new Date()).getTime()) {
      tAction.setStatus(Integer.valueOf(0));
    } else {
      tAction.setStatus(Integer.valueOf(1));
    } 
    if (tAction.getEndDate().getTime() < (new Date()).getTime())
      tAction.setStatus(Integer.valueOf(2)); 
    save(tAction);
    TDoctor tDoctor = this.tDoctorService.get(tAction.getDoctorId().toString());
    Calendar c = Calendar.getInstance();
    c.setTime(tAction.getActionDate());
    int year = c.get(1);
    int month = c.get(2) + 1;
    int day = c.get(5);
    List<TBookNo> bookNoList = new ArrayList<>();
    this.tBookNoService.deleteByParam(tAction.getTenantId(), Integer.valueOf(Integer.parseInt(tAction.getId())), tAction.getDoctorId(), null, null, null);
    if (tAction.getAmStock() != null && tAction.getAmStock().intValue() != 0) {
      if (StringUtils.isBlank(tAction.getAmStartDate()) || StringUtils.isBlank(tAction.getAmEndDate()))
        throw new Exception("上午时间段设置有误"); 
      List<String> times = DateUtils.spliteDate(tAction.getAmStartDate(), tAction.getAmEndDate(), tAction.getAmStock());
      int i = 1;
      for (String time : times) {
        TBookNo tBookNo = new TBookNo();
        tBookNo.setTenantId(tAction.getTenantId());
        tBookNo.setActionId(Integer.valueOf(Integer.parseInt(tAction.getId())));
        tBookNo.setOfficeId(tDoctor.getOfficeId());
        tBookNo.setDoctorId(tAction.getDoctorId());
        tBookNo.setBookYear(Integer.valueOf(year));
        tBookNo.setBookMonth(Integer.valueOf(month));
        tBookNo.setBookDay(Integer.valueOf(day));
        tBookNo.setBookTime(time);
        tBookNo.setBookNo(Integer.valueOf(i));
        tBookNo.setType(Integer.valueOf(1));
        tBookNo.preInsert();
        bookNoList.add(tBookNo);
        i++;
      } 
    } 
    if (tAction.getPmStock() != null && tAction.getPmStock().intValue() != 0) {
      if (StringUtils.isBlank(tAction.getPmStartDate()) || StringUtils.isBlank(tAction.getPmEndDate()))
        throw new Exception("下午时间段设置有误"); 
      List<String> times = DateUtils.spliteDate(tAction.getPmStartDate(), tAction.getPmEndDate(), tAction.getPmStock());
      int i = 1;
      for (String time : times) {
        TBookNo tBookNo = new TBookNo();
        tBookNo.setTenantId(tAction.getTenantId());
        tBookNo.setActionId(Integer.valueOf(Integer.parseInt(tAction.getId())));
        tBookNo.setOfficeId(tDoctor.getOfficeId());
        tBookNo.setDoctorId(tAction.getDoctorId());
        tBookNo.setBookYear(Integer.valueOf(year));
        tBookNo.setBookMonth(Integer.valueOf(month));
        tBookNo.setBookDay(Integer.valueOf(day));
        tBookNo.setBookTime(time);
        tBookNo.setBookNo(Integer.valueOf(i));
        tBookNo.setType(Integer.valueOf(2));
        tBookNo.preInsert();
        bookNoList.add(tBookNo);
        i++;
      } 
    } 
    if (!bookNoList.isEmpty())
      this.tBookNoService.batchInsert(bookNoList); 
  }
  
  @Transactional(readOnly = false)
  public void delete(TAction tAction) {
    super.delete( tAction);
  }
  
  public List<Map<String, Long>> periodConsolidation(List<Map<String, Long>> data) {
    boolean mark = false;
    List<Map<String, Long>> res = (List<Map<String, Long>>)data.stream().collect(Collectors.toList());
    int i;
    label19: for (i = 0; i < data.size(); i++) {
      Map<String, Long> stringLongMap = data.get(i);
      Long startTime = stringLongMap.get("startTime");
      Long endTime = stringLongMap.get("endTime");
      for (int f = i + 1; f < data.size(); f++) {
        Map<String, Long> stringLongMap1 = data.get(f);
        Long startTime1 = stringLongMap1.get("startTime");
        Long endTime1 = stringLongMap1.get("endTime");
        if (startTime.longValue() <= endTime1.longValue() + 1000L && endTime.longValue() + 1000L >= startTime1.longValue()) {
          startTime = Long.valueOf(Math.min(startTime.longValue(), startTime1.longValue()));
          endTime = Long.valueOf(Math.max(endTime1.longValue(), endTime.longValue()));
          res.remove(i);
          res.remove(f - 1);
          Map<String, Long> map = new HashMap<>();
          map.put("startTime", startTime);
          map.put("endTime", endTime);
          res.add(map);
          mark = true;
          break label19;
        } 
      } 
    } 
    if (mark)
      return periodConsolidation(res); 
    return res;
  }
  
  @Transactional(readOnly = false)
  public void actionStatusByIds(List<String> actionIds, int status) {
    ((TActionDao)this.dao).actionStatusByIds(actionIds, status);
  }
  
  @Transactional(readOnly = false)
  public void actionGroupStatusByIds(List<Integer> successActionId, int status) {
    ((TActionDao)this.dao).actionGroupStatusByIds(successActionId, status);
  }
}
